<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>errorreporter.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_debug_errorreporter.js.html">errorreporter.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Definition of the ErrorReporter class, which creates an error
<a name="line17"></a> * handler that reports any errors raised to a URL.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.debug.ErrorReporter&#39;);
<a name="line22"></a>goog.provide(&#39;goog.debug.ErrorReporter.ExceptionEvent&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.debug&#39;);
<a name="line25"></a>goog.require(&#39;goog.debug.ErrorHandler&#39;);
<a name="line26"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line27"></a>goog.require(&#39;goog.debug.entryPointRegistry&#39;);
<a name="line28"></a>goog.require(&#39;goog.events&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line30"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line31"></a>goog.require(&#39;goog.net.XhrIo&#39;);
<a name="line32"></a>goog.require(&#39;goog.object&#39;);
<a name="line33"></a>goog.require(&#39;goog.string&#39;);
<a name="line34"></a>goog.require(&#39;goog.uri.utils&#39;);
<a name="line35"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * Constructs an error reporter. Internal Use Only. To install an error
<a name="line41"></a> * reporter see the {@see #install} method below.
<a name="line42"></a> *
<a name="line43"></a> * @param {string} handlerUrl The URL to which all errors will be reported.
<a name="line44"></a> * @param {function(!Error, !Object.&lt;string, string&gt;)=}
<a name="line45"></a> *     opt_contextProvider When a report is to be sent to the server,
<a name="line46"></a> *     this method will be called, and given an opportunity to modify the
<a name="line47"></a> *     context object before submission to the server.
<a name="line48"></a> * @param {boolean=} opt_noAutoProtect Whether to automatically add handlers for
<a name="line49"></a> *     onerror and to protect entry points.  If apps have other error reporting
<a name="line50"></a> *     facilities, it may make sense for them to set these up themselves and use
<a name="line51"></a> *     the ErrorReporter just for transmission of reports.
<a name="line52"></a> * @constructor
<a name="line53"></a> * @extends {goog.events.EventTarget}
<a name="line54"></a> */
<a name="line55"></a>goog.debug.ErrorReporter = function(
<a name="line56"></a>    handlerUrl, opt_contextProvider, opt_noAutoProtect) {
<a name="line57"></a>  goog.base(this);
<a name="line58"></a>
<a name="line59"></a>  /**
<a name="line60"></a>   * Context provider, if one was provided.
<a name="line61"></a>   * @type {?function(!Error, !Object.&lt;string, string&gt;)}
<a name="line62"></a>   * @private
<a name="line63"></a>   */
<a name="line64"></a>  this.contextProvider_ = opt_contextProvider || null;
<a name="line65"></a>
<a name="line66"></a>  /**
<a name="line67"></a>   * XHR sender.
<a name="line68"></a>   * @type {function(string, string, string, (Object|goog.structs.Map)=)}
<a name="line69"></a>   * @private
<a name="line70"></a>   */
<a name="line71"></a>  this.xhrSender_ = goog.debug.ErrorReporter.defaultXhrSender;
<a name="line72"></a>
<a name="line73"></a>  /**
<a name="line74"></a>   * The URL at which all errors caught by this handler will be logged.
<a name="line75"></a>   *
<a name="line76"></a>   * @type {string}
<a name="line77"></a>   * @private
<a name="line78"></a>   */
<a name="line79"></a>  this.handlerUrl_ = handlerUrl;
<a name="line80"></a>
<a name="line81"></a>  if (!opt_noAutoProtect) {
<a name="line82"></a>    this.setup_();
<a name="line83"></a>  }
<a name="line84"></a>};
<a name="line85"></a>goog.inherits(goog.debug.ErrorReporter, goog.events.EventTarget);
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * Event broadcast when an exception is logged.
<a name="line91"></a> * @param {Error} error The exception that was was reported.
<a name="line92"></a> * @param {!Object.&lt;string, string&gt;} context The context values sent to the
<a name="line93"></a> *     server alongside this error.
<a name="line94"></a> * @constructor
<a name="line95"></a> * @extends {goog.events.Event}
<a name="line96"></a> */
<a name="line97"></a>goog.debug.ErrorReporter.ExceptionEvent = function(error, context) {
<a name="line98"></a>  goog.events.Event.call(this, goog.debug.ErrorReporter.ExceptionEvent.TYPE);
<a name="line99"></a>
<a name="line100"></a>  /**
<a name="line101"></a>   * The error that was reported.
<a name="line102"></a>   * @type {Error}
<a name="line103"></a>   */
<a name="line104"></a>  this.error = error;
<a name="line105"></a>
<a name="line106"></a>  /**
<a name="line107"></a>   * Context values sent to the server alongside this report.
<a name="line108"></a>   * @type {!Object.&lt;string, string&gt;}
<a name="line109"></a>   */
<a name="line110"></a>  this.context = context;
<a name="line111"></a>};
<a name="line112"></a>goog.inherits(goog.debug.ErrorReporter.ExceptionEvent, goog.events.Event);
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * Event type for notifying of a logged exception.
<a name="line117"></a> * @type {string}
<a name="line118"></a> */
<a name="line119"></a>goog.debug.ErrorReporter.ExceptionEvent.TYPE =
<a name="line120"></a>    goog.events.getUniqueId(&#39;exception&#39;);
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * The internal error handler used to catch all errors.
<a name="line125"></a> *
<a name="line126"></a> * @type {goog.debug.ErrorHandler}
<a name="line127"></a> * @private
<a name="line128"></a> */
<a name="line129"></a>goog.debug.ErrorReporter.prototype.errorHandler_ = null;
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * Extra headers for the error-reporting XHR.
<a name="line134"></a> * @type {Object|goog.structs.Map|undefined}
<a name="line135"></a> * @private
<a name="line136"></a> */
<a name="line137"></a>goog.debug.ErrorReporter.prototype.extraHeaders_;
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>/**
<a name="line141"></a> * Logging object.
<a name="line142"></a> *
<a name="line143"></a> * @type {goog.debug.Logger}
<a name="line144"></a> * @private
<a name="line145"></a> */
<a name="line146"></a>goog.debug.ErrorReporter.logger_ =
<a name="line147"></a>    goog.debug.Logger.getLogger(&#39;goog.debug.ErrorReporter&#39;);
<a name="line148"></a>
<a name="line149"></a>
<a name="line150"></a>/**
<a name="line151"></a> * Installs an error reporter to catch all JavaScript errors raised.
<a name="line152"></a> *
<a name="line153"></a> * @param {string} loggingUrl The URL to which the errors caught will be
<a name="line154"></a> *     reported.
<a name="line155"></a> * @param {function(!Error, !Object.&lt;string, string&gt;)=}
<a name="line156"></a> *     opt_contextProvider When a report is to be sent to the server,
<a name="line157"></a> *     this method will be called, and given an opportunity to modify the
<a name="line158"></a> *     context object before submission to the server.
<a name="line159"></a> * @param {boolean=} opt_noAutoProtect Whether to automatically add handlers for
<a name="line160"></a> *     onerror and to protect entry points.  If apps have other error reporting
<a name="line161"></a> *     facilities, it may make sense for them to set these up themselves and use
<a name="line162"></a> *     the ErrorReporter just for transmission of reports.
<a name="line163"></a> * @return {goog.debug.ErrorReporter} The error reporter.
<a name="line164"></a> */
<a name="line165"></a>goog.debug.ErrorReporter.install = function(
<a name="line166"></a>    loggingUrl, opt_contextProvider, opt_noAutoProtect) {
<a name="line167"></a>  var instance = new goog.debug.ErrorReporter(
<a name="line168"></a>      loggingUrl, opt_contextProvider, opt_noAutoProtect);
<a name="line169"></a>  return instance;
<a name="line170"></a>};
<a name="line171"></a>
<a name="line172"></a>
<a name="line173"></a>/**
<a name="line174"></a> * Default implemntation of XHR sender interface.
<a name="line175"></a> *
<a name="line176"></a> * @param {string} uri URI to make request to.
<a name="line177"></a> * @param {string} method Send method.
<a name="line178"></a> * @param {string} content Post data.
<a name="line179"></a> * @param {Object|goog.structs.Map=} opt_headers Map of headers to add to the
<a name="line180"></a> *     request.
<a name="line181"></a> */
<a name="line182"></a>goog.debug.ErrorReporter.defaultXhrSender = function(uri, method, content,
<a name="line183"></a>    opt_headers) {
<a name="line184"></a>  goog.net.XhrIo.send(uri, null, method, content, opt_headers);
<a name="line185"></a>};
<a name="line186"></a>
<a name="line187"></a>
<a name="line188"></a>/**
<a name="line189"></a> * Installs exception protection for an entry point function in addition
<a name="line190"></a> * to those that are protected by default.
<a name="line191"></a> * Has no effect in IE because window.onerror is used for reporting
<a name="line192"></a> * exceptions in that case.
<a name="line193"></a> *
<a name="line194"></a> * @param {Function} fn An entry point function to be protected.
<a name="line195"></a> * @return {Function} A protected wrapper function that calls the entry point
<a name="line196"></a> *     function or null if the entry point could not be protected.
<a name="line197"></a> */
<a name="line198"></a>goog.debug.ErrorReporter.prototype.protectAdditionalEntryPoint = function(fn) {
<a name="line199"></a>  if (this.errorHandler_) {
<a name="line200"></a>    return this.errorHandler_.protectEntryPoint(fn);
<a name="line201"></a>  }
<a name="line202"></a>  return null;
<a name="line203"></a>};
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * Add headers to the logging url.
<a name="line208"></a> * @param {Object|goog.structs.Map} loggingHeaders Extra headers to send
<a name="line209"></a> *     to the logging URL.
<a name="line210"></a> */
<a name="line211"></a>goog.debug.ErrorReporter.prototype.setLoggingHeaders =
<a name="line212"></a>    function(loggingHeaders) {
<a name="line213"></a>  this.extraHeaders_ = loggingHeaders;
<a name="line214"></a>};
<a name="line215"></a>
<a name="line216"></a>
<a name="line217"></a>/**
<a name="line218"></a> * Set the function used to send error reports to the server.
<a name="line219"></a> * @param {function(string, string, string, (Object|goog.structs.Map)=)}
<a name="line220"></a> *     xhrSender If provided, this will be used to send a report to the
<a name="line221"></a> *     server instead of the default method. The function will be given the URI,
<a name="line222"></a> *     HTTP method request content, and (optionally) request headers to be
<a name="line223"></a> *     added.
<a name="line224"></a> */
<a name="line225"></a>goog.debug.ErrorReporter.prototype.setXhrSender = function(xhrSender) {
<a name="line226"></a>  this.xhrSender_ = xhrSender;
<a name="line227"></a>};
<a name="line228"></a>
<a name="line229"></a>
<a name="line230"></a>/**
<a name="line231"></a> * Sets up the error reporter.
<a name="line232"></a> *
<a name="line233"></a> * @private
<a name="line234"></a> */
<a name="line235"></a>goog.debug.ErrorReporter.prototype.setup_ = function() {
<a name="line236"></a>  if (goog.userAgent.IE) {
<a name="line237"></a>    // Use &quot;onerror&quot; because caught exceptions in IE don&#39;t provide line number.
<a name="line238"></a>    goog.debug.catchErrors(
<a name="line239"></a>        goog.bind(this.handleException, this), false, null);
<a name="line240"></a>  } else {
<a name="line241"></a>    // &quot;onerror&quot; doesn&#39;t work with FF2 or Chrome
<a name="line242"></a>    this.errorHandler_ = new goog.debug.ErrorHandler(
<a name="line243"></a>        goog.bind(this.handleException, this));
<a name="line244"></a>
<a name="line245"></a>    this.errorHandler_.protectWindowSetTimeout();
<a name="line246"></a>    this.errorHandler_.protectWindowSetInterval();
<a name="line247"></a>    goog.debug.entryPointRegistry.monitorAll(this.errorHandler_);
<a name="line248"></a>  }
<a name="line249"></a>};
<a name="line250"></a>
<a name="line251"></a>
<a name="line252"></a>/**
<a name="line253"></a> * Handler for caught exceptions. Sends report to the LoggingServlet and
<a name="line254"></a> * notifies any listeners.
<a name="line255"></a> *
<a name="line256"></a> * @param {Object} e The exception.
<a name="line257"></a> * @param {!Object.&lt;string, string&gt;=} opt_context Context values to optionally
<a name="line258"></a> *     include in the error report.
<a name="line259"></a> */
<a name="line260"></a>goog.debug.ErrorReporter.prototype.handleException = function(e,
<a name="line261"></a>    opt_context) {
<a name="line262"></a>  var error = /** @type {!Error} */ (goog.debug.normalizeErrorObject(e));
<a name="line263"></a>
<a name="line264"></a>  // Construct the context, possibly from the one provided in the argument, and
<a name="line265"></a>  // pass it to the context provider if there is one.
<a name="line266"></a>  var context = opt_context ? goog.object.clone(opt_context) : {};
<a name="line267"></a>  if (this.contextProvider_) {
<a name="line268"></a>    try {
<a name="line269"></a>      this.contextProvider_(error, context);
<a name="line270"></a>    } catch (err) {
<a name="line271"></a>      goog.debug.ErrorReporter.logger_.severe(&#39;Context provider threw an &#39; +
<a name="line272"></a>          &#39;exception: &#39; + err.message);
<a name="line273"></a>    }
<a name="line274"></a>  }
<a name="line275"></a>  this.sendErrorReport(error.message, error.fileName, error.lineNumber,
<a name="line276"></a>      error.stack, context);
<a name="line277"></a>
<a name="line278"></a>  try {
<a name="line279"></a>    this.dispatchEvent(
<a name="line280"></a>        new goog.debug.ErrorReporter.ExceptionEvent(error, context));
<a name="line281"></a>  } catch (ex) {
<a name="line282"></a>    // Swallow exception to avoid infinite recursion.
<a name="line283"></a>  }
<a name="line284"></a>};
<a name="line285"></a>
<a name="line286"></a>
<a name="line287"></a>/**
<a name="line288"></a> * Sends an error report to the logging URL.  This will not consult the context
<a name="line289"></a> * provider, the report will be sent exactly as specified.
<a name="line290"></a> *
<a name="line291"></a> * @param {string} message Error description.
<a name="line292"></a> * @param {string} fileName URL of the JavaScript file with the error.
<a name="line293"></a> * @param {number} line Line number of the error.
<a name="line294"></a> * @param {string=} opt_trace Call stack trace of the error.
<a name="line295"></a> * @param {!Object.&lt;string, string&gt;=} opt_context Context information to include
<a name="line296"></a> *     in the request.
<a name="line297"></a> */
<a name="line298"></a>goog.debug.ErrorReporter.prototype.sendErrorReport =
<a name="line299"></a>    function(message, fileName, line, opt_trace, opt_context) {
<a name="line300"></a>  try {
<a name="line301"></a>    // Create the logging URL.
<a name="line302"></a>    var requestUrl = goog.uri.utils.appendParams(this.handlerUrl_,
<a name="line303"></a>        &#39;script&#39;, fileName, &#39;error&#39;, message, &#39;line&#39;, line);
<a name="line304"></a>    var queryMap = {};
<a name="line305"></a>    queryMap[&#39;trace&#39;] = opt_trace;
<a name="line306"></a>
<a name="line307"></a>    // Copy context into query data map
<a name="line308"></a>    if (opt_context) {
<a name="line309"></a>      for (var entry in opt_context) {
<a name="line310"></a>        queryMap[&#39;context.&#39; + entry] = opt_context[entry];
<a name="line311"></a>      }
<a name="line312"></a>    }
<a name="line313"></a>
<a name="line314"></a>    // Copy query data map into request.
<a name="line315"></a>    var queryData = goog.uri.utils.buildQueryDataFromMap(queryMap);
<a name="line316"></a>
<a name="line317"></a>    // Send the request with the contents of the error.
<a name="line318"></a>    this.xhrSender_(requestUrl, &#39;POST&#39;, queryData, this.extraHeaders_);
<a name="line319"></a>  } catch (e) {
<a name="line320"></a>    var logMessage = goog.string.buildString(
<a name="line321"></a>        &#39;Error occurred in sending an error report.\n\n&#39;,
<a name="line322"></a>        &#39;script:&#39;, fileName, &#39;\n&#39;,
<a name="line323"></a>        &#39;line:&#39;, line, &#39;\n&#39;,
<a name="line324"></a>        &#39;error:&#39;, message, &#39;\n&#39;,
<a name="line325"></a>        &#39;trace:&#39;, opt_trace);
<a name="line326"></a>    goog.debug.ErrorReporter.logger_.info(logMessage);
<a name="line327"></a>  }
<a name="line328"></a>};
<a name="line329"></a>
<a name="line330"></a>
<a name="line331"></a>/** @override */
<a name="line332"></a>goog.debug.ErrorReporter.prototype.disposeInternal = function() {
<a name="line333"></a>  goog.dispose(this.errorHandler_);
<a name="line334"></a>  goog.base(this, &#39;disposeInternal&#39;);
<a name="line335"></a>};
</pre>


</body>
</html>
